//
// Copyright 2022 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// Selector '.mdc-*' should only be used in this project.
// stylelint-disable selector-class-pattern --
// Internal styling for Tooltip MDC component.

@use '@material/button/button-text-theme';
@use '@material/elevation/elevation-theme';
@use '@material/theme/keys';
@use '@material/theme/theme';
@use '@material/tokens/resolvers';
@use '@material/typography/typography';
@use './tooltip-theme';
@use 'sass:map';
@use 'sass:meta';

$custom-property-prefix: 'rich-tooltip';
$light-theme: (
  action-focus-label-text-color: null,
  action-focus-state-layer-color: null,
  action-focus-state-layer-opacity: null,
  action-hover-label-text-color: null,
  action-hover-state-layer-color: null,
  action-hover-state-layer-opacity: null,
  action-label-text-color: null,
  action-label-text-font: null,
  action-label-text-line-height: null,
  action-label-text-size: null,
  action-label-text-tracking: null,
  action-label-text-type: null,
  action-label-text-weight: null,
  action-pressed-label-text-color: null,
  action-pressed-state-layer-color: null,
  action-pressed-state-layer-opacity: null,
  container-color: null,
  container-elevation: null,
  container-shadow-color: null,
  container-shape: null,
  container-surface-tint-layer-color: null,
  content-overflow-y: null,
  content-overflow-x: null,
  subhead-color: null,
  subhead-font: null,
  subhead-line-height: null,
  subhead-size: null,
  subhead-tracking: null,
  subhead-type: null,
  subhead-weight: null,
  supporting-text-color: null,
  supporting-text-font: null,
  supporting-text-line-height: null,
  supporting-text-size: null,
  supporting-text-tracking: null,
  supporting-text-type: null,
  supporting-text-weight: null,
);

@mixin theme($theme, $resolvers: resolvers.$material) {
  @include theme.validate-theme($light-theme, $theme);
  $theme: _resolve-theme($theme, $resolvers);
  @include keys.declare-custom-properties(
    $theme,
    $prefix: $custom-property-prefix
  );
}

@mixin theme-styles($theme, $resolvers: resolvers.$material) {
  @include theme.validate-theme-styles($light-theme, $theme);
  // Only resolve typography tokens because the elevation theme mixin handles
  // resolving elevation tokens internally.
  $theme: _resolve-theme-typography($theme, map.get($resolvers, 'typography'));
  $theme: keys.create-theme-properties(
    $theme,
    $prefix: $custom-property-prefix
  );

  @include tooltip-theme.rich-fill-color(map.get($theme, 'container-color'));
  &.mdc-tooltip--rich {
    @include tooltip-theme.surface-shape-radius(
      map.get($theme, 'container-shape')
    );
  }
  @include tooltip-theme.rich-text-ink-color(
    $title-color: map.get($theme, 'subhead-color'),
    $content-color: map.get($theme, 'supporting-text-color'),
    $content-link-color: map.get($theme, 'action-label-text-color')
  );

  .mdc-tooltip--rich-action {
    @include button-text-theme.theme-styles(
      (
        focus-label-text-color: map.get($theme, 'action-focus-label-text-color'),
        focus-state-layer-color:
          map.get($theme, 'action-focus-state-layer-color'),
        focus-state-layer-opacity:
          map.get($theme, 'action-focus-state-layer-opacity'),
        hover-label-text-color: map.get($theme, 'action-hover-label-text-color'),
        hover-state-layer-color:
          map.get($theme, 'action-hover-state-layer-color'),
        hover-state-layer-opacity:
          map.get($theme, 'action-hover-state-layer-opacity'),
        label-text-color: map.get($theme, 'action-label-text-color'),
        label-text-font: map.get($theme, 'action-label-text-font'),
        label-text-size: map.get($theme, 'action-label-text-size'),
        label-text-tracking: map.get($theme, 'action-label-text-tracking'),
        label-text-weight: map.get($theme, 'action-label-text-weight'),
        pressed-label-text-color:
          map.get($theme, 'action-pressed-label-text-color'),
        pressed-state-layer-color:
          map.get($theme, 'action-pressed-state-layer-color'),
        pressed-state-layer-opacity:
          map.get($theme, 'action-pressed-state-layer-opacity'),
      )
    );
  }

  @include _container-elevation(map.get($resolvers, 'elevation'), $theme);
  @include tooltip-theme.content-scroll(
    map.get($theme, 'content-overflow-x'),
    map.get($theme, 'content-overflow-y')
  );

  .mdc-tooltip__surface .mdc-tooltip__title {
    @include typography.theme-styles(
      (
        font: map.get($theme, 'subhead-font'),
        line-height: map.get($theme, 'subhead-line-height'),
        size: map.get($theme, 'subhead-size'),
        weight: map.get($theme, 'subhead-weight'),
        tracking: map.get($theme, 'subhead-tracking'),
      )
    );
  }
}

@mixin _container-elevation($elevation-resolver, $theme) {
  .mdc-tooltip__surface {
    @include elevation-theme.with-resolver(
      $elevation-resolver,
      $elevation: map.get($theme, 'container-elevation'),
      $shadow-color: map.get($theme, 'container-shadow-color')
    );

    @include elevation-theme.overlay-container-color(
      map.get($theme, 'container-surface-tint-layer-color')
    );
  }

  .mdc-tooltip__caret-surface-bottom {
    @include elevation-theme.with-resolver(
      $elevation-resolver,
      $elevation: map.get($theme, 'container-elevation'),
      $shadow-color: map.get($theme, 'container-shadow-color')
    );
  }

  // The top caret surface needs to matching surface elevation styles as the
  // rich tooltip *without* the shadow beneath it (the caret is a diamond
  // overlaid on top of the tooltip surface, a shadow on this element would
  // therefore be visible on top of the tooltip). To accomplish this we must
  // resolve the elevation values first and then call each mixin individually
  // (since the theme mixin does not support custom properties).
  .mdc-tooltip__caret-surface-top {
    $overlay-opacity: _generate-elevation-overlay-opacity(
      $theme,
      $elevation-resolver
    );
    @include elevation-theme.overlay-opacity($overlay-opacity);
    @include elevation-theme.overlay-container-color(
      map.get($theme, 'container-surface-tint-layer-color')
    );
  }
}

@function _generate-elevation-overlay-opacity($theme, $elevation-resolver) {
  @if map.get($theme, 'container-elevation') and
    map.get($theme, 'container-shadow-color')
  {
    $resolved-value: meta.call(
      $elevation-resolver,
      $elevation: map.get($theme, 'container-elevation'),
      $shadow-color: map.get($theme, 'container-shadow-color')
    );
    @return map.get($resolved-value, overlay-opacity);
  }

  @return null;
}

@function _resolve-theme-elevation($theme, $elevation-resolver) {
  @if map.get($theme, 'container-elevation') and
    map.get($theme, 'container-shadow-color')
  {
    $resolved-value: meta.call(
      $elevation-resolver,
      $elevation: map.get($theme, 'container-elevation'),
      $shadow-color: map.get($theme, 'container-shadow-color')
    );
    $theme: map.set($theme, 'container-elevation', $resolved-value);
  }

  @return $theme;
}

@function _resolve-theme-typography($theme, $resolver) {
  @return typography.resolve-theme(
    $theme,
    $resolver,
    action-label-text,
    supporting-text
  );
}

@function _resolve-theme($theme, $resolvers) {
  $theme: map.merge(
    $theme,
    _resolve-theme-elevation($theme, map.get($resolvers, 'elevation'))
  );

  $theme: map.merge(
    $theme,
    _resolve-theme-typography($theme, map.get($resolvers, 'typography'))
  );
  @return $theme;
}
